<template>
  <div class="login-container">
    <img src="../../icons/images/11.png" class="img2Style" />
    <div class="containerStyle">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <img src="../../icons/images/logo.jpg" class="imgStyle" />
          <h3 class="title">"艾吃"外卖</h3>
        </div>

        <el-form-item prop="username" class="inputStyle">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
            class="borderNone"
          />
        </el-form-item>

        <el-form-item prop="password" class="inputStyle">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
            class="borderNone"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          class="buttonStyle"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("Please enter the correct user name"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("The password can not be less than 6 digits"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: black;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  width: 100%;
  height: 100%;
}
.el-input {
  display: inline-block;
  height: 55px;
  width: 63%;
  input {
    background: transparent;
    height: 55px;
    border-bottom:1px solid rgba(0,0,0,.1);
    // caret-color: $cursor;

    &:-webkit-autofill {
      box-shadow: 0 0 0px 1000px $bg inset !important;
      -webkit-text-fill-color: $cursor !important;
    }
  }
}
// .el-form-item {
//   border: 1px solid rgba(255, 255, 255, 0.1);
//   background: rgba(0, 0, 0, 0.1);
//   border-radius: 5px;
//   color: #454545;
// }
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  display: flex;
  min-height: 100%;
  width: 100%;
  background-color: #283443;
  overflow: hidden;
  .containerStyle {
    margin-top: 220px;
    margin-left: -90px;
    width: 23%;
    height: 58%;
    background-color: white;
    border-radius: 10px;
    box-shadow: 10px -5px 20px 2px rgba(252, 157, 49, 0.671);
  }
  input{
    width: 200px;
    padding: 12px 20px 12px 30px;
  }
  .inputStyle {
    margin: 35px 0px 20px 10px;
    background-color: transparent;
  }
  .img2Style {
    margin-top: 100px;
    margin-left: 280px;
    width: 38%;
    height: 83%;
    z-index: 99;
  }
  .login-form {
    position: relative;
    width: 100%;
    max-width: 100%;
    padding: 65px 30px 10px 70px;
    margin: 0 auto;
    overflow: hidden;
  }
  .svg-container {
    padding: 6px 5px 6px 5px;
    color: rgba(74, 47, 47, 0.1);
    vertical-align: middle;
    font-size: 20px;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    display: flex;
    position: relative;
    .imgStyle {
      margin-top:10px;
      margin-left: 10px;
      width: 73px;
      height: 65px;
    }

    .title {
      font-size: 26px;
      color: black;
      margin-top: 30px;
      margin-left: 10px;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 70px;
    top: 7px;
    font-size: 16px;
    color: pink;
    cursor: pointer;
    user-select: none;
  }
  .buttonStyle {
    margin-top: 35px;
    margin-left: 30px;
    border: none;
    background-color: pink;
    width: 200px !important;
    border-radius: 50px;
  }
}
</style>
